package cn.upfinder.allvideo.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tencent.smtt.sdk.TbsVideo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.upfinder.allvideo.Activity.VideoDetailActivity;
import cn.upfinder.allvideo.Adapter.MovieListAdapter;
import cn.upfinder.allvideo.Contract.VideoListContract;
import cn.upfinder.allvideo.Presenter.VideoListPresenter;
import cn.upfinder.allvideo.R;
import cn.upfinder.datalibrary.Api.JXApi;
import cn.upfinder.datalibrary.Bean.VideoEntity;
import cn.upfinder.datalibrary.Config.VideoType;
import cn.upfinder.uilibrary.Adapter.BaseQuickAdapter;


public class VideoListFragment extends Fragment implements VideoListContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private static final String ARG_PARAM = "param1";
    public static final int TYPE_YOUKU_MOVIE = 11;
    public static final int TYPE_YOUKU_PLAY = 13;
    public static final int TYPE_SOUKU_BEAUTY_DANCE = 14;

    @BindView(R.id.rvContent)
    RecyclerView rvContent;

    @BindView(R.id.srlContent)
    SwipeRefreshLayout srlContent;

    private int showVideoType;

    private VideoListPresenter mvpPresenter;
    private int currentPage;

    private MovieListAdapter adapter;


    public VideoListFragment() {
        // Required empty public constructor
    }


    public static VideoListFragment newInstance(int showVideoType) {
        VideoListFragment fragment = new VideoListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM, showVideoType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            showVideoType = getArguments().getInt(ARG_PARAM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.swipe_relative_layout, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
        initData();

    }

    protected void initView() {

        srlContent.setRefreshing(true);
        rvContent.setLayoutManager(new GridLayoutManager(getContext(), 2));
        rvContent.setHasFixedSize(true);
        adapter = new MovieListAdapter(R.layout.item_video_layout, null);
        adapter.openLoadAnimation();
        adapter.openLoadMore(15, true);

        rvContent.setAdapter(adapter);
    }

    protected void initListener() {
        srlContent.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this);
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getContext(), VideoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(VideoDetailActivity.BUNDLE_KEY, adapter.getItem(position));
                intent.putExtra(VideoDetailActivity.BUNDLE_KEY, bundle);
                startActivity(intent);

            }
        });
    }

    protected void initData() {
        currentPage = 1;
        mvpPresenter = (VideoListPresenter) getMvpPresenter();
        switch (showVideoType) {
            case TYPE_YOUKU_MOVIE: //优酷电影
                mvpPresenter.getMovies(currentPage);
                break;
            case TYPE_YOUKU_PLAY: //优酷综艺
                mvpPresenter.getYouKuPlay(currentPage);
                break;
            case TYPE_SOUKU_BEAUTY_DANCE: //搜酷美女舞蹈
                mvpPresenter.getSouKuBeautyDance(currentPage);
                break;
        }

    }

    @Override
    public void showMovie(ArrayList<VideoEntity> youKuMovies) {
        adapter.setNewData(youKuMovies);
        srlContent.setRefreshing(false);

    }

    @Override
    public void showMoreMovie(ArrayList<VideoEntity> youKuMovies) {
        adapter.notifyDataChangedAfterLoadMore(youKuMovies, true);
    }

    @Override
    public VideoListContract.Presenter getMvpPresenter() {
        if (mvpPresenter == null) {
            mvpPresenter = new VideoListPresenter(getContext(), this);
        }
        return mvpPresenter;
    }

    @Override
    public void showError(String errorMsg) {
        srlContent.setRefreshing(false);
    }

    @Override
    public void onLoadMoreRequested() {
        currentPage++;
        switch (showVideoType) {
            case TYPE_YOUKU_MOVIE:
                mvpPresenter.getMoreMovies(currentPage);
                break;
            case TYPE_YOUKU_PLAY:
                mvpPresenter.getYouKuPlay(currentPage);
                break;
            case TYPE_SOUKU_BEAUTY_DANCE:
                mvpPresenter.getMoreSouKuBeautyDance(currentPage);
                break;
        }

    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        switch (showVideoType) {
            case TYPE_YOUKU_MOVIE:
                mvpPresenter.getMovies(currentPage);
                break;
            case TYPE_YOUKU_PLAY:
                break;
            case TYPE_SOUKU_BEAUTY_DANCE:
                mvpPresenter.getSouKuBeautyDance(currentPage);
                break;
        }


    }
}
